import redis
import time
import random


class TRedis(object):
    def __init__(self, host, port, password):
        self.r = redis.Redis(host=host, port=port, password=password)
        # print(type(self.r))
        # self.test_string()
        # self.r.set("name","123456")
        # print(self.r.get("name"))

    def test_string(self):
        self.r.set("name", "123456")
        print(self.r.get("name"))
        self.r.delete("name")
        print(self.r.get("name"))

    def test_hash(self):
        self.r.hset("test", "1", "qwerty")
        print(self.r.hgetall("test"))
        self.r.hdel("test", "1")
        print(self.r.hgetall("test"))
        self.r.hdel("test")
        print(self.r.hgetall("test"))

    def insert_hash_ten_thousand(self):
        a = time.time()
        i = 0
        try:
            for i in range(1, 2):
                self.r.hset("test", i, "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000")
                # print(i)
        except redis.exceptions.ResponseError:
            print("123")
        print(time.time() - a)

    def insert_service_ac(self):
        for i in range(1, 6):
            self.r.sadd("service", "ac%s" % i)
        self.show_set()

    def delete_service_ac(self):
        self.show_set()
        set_num = self.r.scard("service")
        for i in range(0, set_num):
            self.r.spop("service")
        self.show_set()

    def show_set(self):
        print(self.r.scard("service"))
        print(self.r.smembers("service"))

    def insert_list_many(self, insert_count=1):
        a = time.time()
        for i in range(0, insert_count):
            self.r.rpush("ac%s" % random.randint(1, 5),
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000",
                         "ip=192.168.1.100&ac_ip=192.168.1.199&port=2000")
            # print("插入%s条数据花费的时间：" % str(insert_count) + str(time.time() - a))
            # self.show_list_all()
            # self.delete_list_all()

    def show_list_all(self):
        for i in range(1, 6):
            print("------ac%s------" % i)
            self.show_list("ac%s" % i)

    def show_list(self, name="ac1"):
        list_len = self.r.llen(name)
        print("%s长度:" % name + str(list_len))
        # print("%s内容:" % name + str(self.r.lrange(name, 0, -1)))

    def delete_list_all(self):
        print("----------------开始删除----------------")
        a = time.time()
        for i in range(1, 6):
            print("--------ac%s--------" % i)
            self.delete_list("ac%s" % i)
        print("插入数据花费的时间：" + str(time.time() - a))

    def delete_list(self, name):
        print("删除前：")
        self.show_list(name)
        list_len = self.r.llen(name)
        self.r.ltrim(name, list_len + 1, 0)
        print("删除后：")
        self.show_list(name)


if __name__ == '__main__':
    TRedis = TRedis("192.168.1.238", "6379", "123456")
    i = 1
    count = 0
    while (i < 10000):
        TRedis.insert_list_many(400)
        i = i + 1
        print(i)
        count += 400 * 247
        print(count)
        # TRedis.delete_list_all()
        # TRedis.show_set()
        # TRedis.show_list_all()
